local app = require 'app'
local game = app.game.running
local sound_effect = game.require 'ui.ingame.sound_effect'

local seats = game.ui.element()

local log = game.logging.get("seats")

seats.store_watchers = {
    room = {
        ["$"] = function(self, store, state)
            for no, player in pairs(state.playing_players) do
                self:enter(player)
            end
        end,

        ["玩家进入"] = function(self, store, state, player)
            self:enter(player);
        end,

        ["玩家离开"] = function(self, store, state, player)
            self:leave(player);
        end,

        ['更新准备状态'] = function(self, store, state, player)
            self:update_state(player)
        end
    },

    ingame = {
        ["$"] = function()
        end,

        ["玩家出牌"] = function(self, store, state, args)
            local player = args.player
            local card_ids = args.card_ids

            self:handout(player, card_ids)
        end,

        ["请求玩家出牌"] = function(self, store, state, args)
            local player = args.player
            local time = args.time
            local round_first = args.round_first

            self:on_handout(player, time, round_first)
        end,

        ['玩家叫分'] = function(self, store, state, args)
            local player = args.player
            local action_id = args.action_id

            self:on_bid_result(player, action_id)
        end,

        ['通知地主'] = function(self, store, state)
            local player = state.lord_player
            self:on_notice_lord(player)
        end,

        ["流局"] = function(self, store, state)
            self:restart()
        end
    }
}

local function find_seat(self, player)
    if player.seat and player.seat.no > 0 then
        if not player.seat.local_no then
            log.error("错误？")
        end
        local seat = self[player.seat.local_no]
        if not seat then
            log.error("座位号越界："..player.seat.no.."local_no:"..tostring(player.seat.local_no));
        else
            return seat
        end
    end
    log.error(string.format("player.seat=%s no=%s", tostring(player.seat), tostring(player.seat.no)));
    return nil
end

function seats:ctor(ingame)
    self.ingame = ingame
    self.res = "club.threecard.doudizhu/ui/ingame.b:seats"
    self.max = 3

    local seat = game.require "ui.ingame.seat"
    for i = 1, self.max do
        self[i] = seat.new(i)
        self[i]:set_data(nil);
    end
end

function seats:load()
    self:super("load");
    for i = 1, self.max do
        local root = self.transform:Find("seat_"..i);
        self[i]:load(root);
        if not root then
            log.error("初始化座位信息错误  座位号："..i)
        end
    end
end

function seats:enter(player)
    local seat = find_seat(self, player)
    if seat then
        seat:set_data(player)
    end
end

function seats:leave(player)
    local seat = find_seat(self, player)
    if seat then
        seat:set_data(nil)
    end
end

function seats:update_state(player)
    local seat = find_seat(self, player)
    if seat then
        seat:ready_change(player.is_ready==1)
    end
end

function seats:on_bid_result(player, action_id)
    local seat = find_seat(self, player)
    if seat then
        seat.bid_result:on_result(action_id)
        sound_effect.bid_result(player, action_id)
    end
end

function seats:on_notice_lord(player)
    for i = 1, self.max do
        local seat = self[i]
        seat:mark_lord(seat.player == player)
    end
end

function seats:on_handout(player, time, round_first)
    if round_first then
        for i = 1, self.max do
            local seat = self[i]
            seat:on_handout()
        end
    end

    local seat = find_seat(self, player)
    if seat and seat.local_no ~= 1 then
        self.ingame.clock:start(time, seat.clock_root)
        seat:on_handout()
    end
end

function seats:handout(player, card_ids)
    local seat = find_seat(self, player)
    if seat then
        seat:handout(card_ids)
        sound_effect.player_handout(player, card_ids)
    end
end

function seats:restart()
    for i = 1, self.max do
        local seat = self[i]
        seat.poker_container:clear()
    end
end

function seats:clear_scene()
    for i = 1, self.max do
        local seat = self[i]
        seat:clear_scene()
    end
end

return seats